package com.nnxy.service.impl;

import com.nnxy.mapper.EmpMapper;
import com.nnxy.service.DeptService;
import com.nnxy.mapper.DeptMapper;
import com.nnxy.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> list() {
        return deptMapper.findAll();
    }

    @Override
    public void removeById(Integer id) {
        deptMapper.deleteById(id);

    }

    @Override
    public void add(Dept dept) {
        //dept是前端传递数据封装的对象，只有name属性有值，其他属性都是null
    //1.补全数据（创建时间和修改时间）
    dept.setCreateTime(LocalDateTime.now());
    dept.setUpdateTime(LocalDateTime.now());

    //2.调用mapper将数据插入到数据库
    deptMapper.insert(dept);
    }

    @Override
    public Dept findById(Integer id) {
        Dept dept=deptMapper.findById(id);
        return dept;
    }

    @Override
    public void modify(Dept dept) {
//        获取修改后的时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
    @Override
    public boolean hasEmployees(Integer deptId) {
        int count = empMapper.deleteByDeptId(deptId);
        return count > 0;
    }

}

